package cn.sykj.car.utils;

import org.apache.commons.lang3.math.NumberUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static void main(String[] args) throws Exception {
        System.out.println(getCurFirstDay().toString());
        System.out.println(getCurLastDay().toString());
//        System.out.println(getBetweenDay("2021-05-31 00:00:00","2021-06-01 23:59:59"));
        System.out.println(getBetweenHour("2021-05-31 00:00:00","2021-06-01 00:00:01"));
        String yyyyMMDD = "2021-05-31*00:00:00";
        System.out.println(yyyyMMDD.substring(0,9));
        System.out.println(yyyyMMDD.substring(0,10));
        System.out.println(yyyyMMDD.substring(0,11));
    }
    public static String getPreviousMonth(String dateStr){
        Date date = null;
        try {
            date = org.apache.commons.lang3.time.DateUtils.parseDate(dateStr, "yyyy-MM-dd");
            Date newDate = org.apache.commons.lang3.time.DateUtils.addMonths(date, -1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String newDateStr = sdf.format(newDate);
            return newDateStr;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Date getCurFirstDay() throws ParseException {
        String ymd = getCurYearMonth().concat("-01");
        Date startDate = org.apache.commons.lang3.time.DateUtils.parseDate(ymd.concat(" 00:00:00"), "yyyy-MM-dd HH:mm:ss");
        return startDate;
    }
    public static Date getCurLastDay() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String ymd = sdf.format(calendar.getTime());
        Date endDate = org.apache.commons.lang3.time.DateUtils.parseDate(ymd.concat(" 23:59:59"), "yyyy-MM-dd HH:mm:ss");
        endDate = org.apache.commons.lang3.time.DateUtils.addDays(endDate, -1);
        return endDate;
    }
    public static Date getFirstDay(String calcDateDay, String yearMonth) throws ParseException {
        String date = yearMonth + "-" + calcDateDay;
        String previousMonth = DateUtils.getPreviousMonth(date);
        Date startDate = org.apache.commons.lang3.time.DateUtils.parseDate(previousMonth.concat(" 00:00:00"), "yyyy-MM-dd HH:mm:ss");
        return startDate;
    }
    public static Date getLastDay(String calcDateDay,String yearMonth) throws ParseException {
        String date = yearMonth + "-" + calcDateDay;
        Date endDate = org.apache.commons.lang3.time.DateUtils.parseDate(date.concat(" 23:59:59"), "yyyy-MM-dd HH:mm:ss");
        endDate = org.apache.commons.lang3.time.DateUtils.addDays(endDate, -1);
        return endDate;
    }
    public static String getFirstDayStr(String calcDateDay, String yearMonth) throws ParseException {
        String date = yearMonth + "-" + calcDateDay;
        String previousMonth = DateUtils.getPreviousMonth(date);
        Date startDate = org.apache.commons.lang3.time.DateUtils.parseDate(previousMonth.concat(" 00:00:00"), "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(startDate);
    }
    public static String getLastDayStr(String calcDateDay,String yearMonth) throws ParseException {
        String date = yearMonth + "-" + calcDateDay;
        Date endDate = org.apache.commons.lang3.time.DateUtils.parseDate(date.concat(" 23:59:59"), "yyyy-MM-dd HH:mm:ss");
        endDate = org.apache.commons.lang3.time.DateUtils.addDays(endDate, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(endDate);
    }
    public static String getCurYearMonth(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(new Date());
    }
    public static int getCurYear(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return NumberUtils.toInt(sdf.format(new Date()));
    }
    public static int getCurMonth(){
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return NumberUtils.toInt(sdf.format(new Date()));
    }
    public static String get7DayDateTime(){
    	Date date = org.apache.commons.lang3.time.DateUtils.addDays(new Date(), -6);
    	SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyy-MM-dd");
    	String ymd = sdf_ymd.format(date);
    	return ymd.concat(" 00:00:00");
    }
    public static String getCurDateTime(){
    	SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyy-MM-dd");
    	String ymd = sdf_ymd.format(new Date());
    	return ymd.concat(" 23:59:59");
    }

    /*
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
    */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        //设置结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        //处于开始时间之后，和结束时间之前的判断
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取两个时间的天数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getBetweenDay(Date startTime,Date endTime){
        long between = endTime.getTime() - startTime.getTime();
        return between / (24 * 60 * 60 * 1000);
    }
    public static long getBetweenDay(String startTime,String endTime) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = format.parse(startTime);
        Date endDate = format.parse(endTime);
        long between = endDate.getTime() - startDate.getTime();
        return between / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取两个时间的小时数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getBetweenHour(Date startTime,Date endTime){
        long between = endTime.getTime() - startTime.getTime();
        return between / (60 * 60 * 1000);
    }
    public static long getBetweenHour(String startTime,String endTime) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = format.parse(startTime);
        Date endDate = format.parse(endTime);
        long between = endDate.getTime() - startDate.getTime();
        return between / (60 * 60 * 1000);
    }

    /**
     * 获取两个时间的分钟数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getBetweenMinute(Date startTime,Date endTime){
        long between = endTime.getTime() - startTime.getTime();
        return between / (60 * 1000);
    }
    public static long getBetweenMinute(String startTime,String endTime) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = format.parse(startTime);
        Date endDate = format.parse(endTime);
        long between = endDate.getTime() - startDate.getTime();
        return between / (60 * 1000);
    }

    /**
     * 获取两个时间的秒数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getBetweenSecond(Date startTime,Date endTime){
        long between = endTime.getTime() - startTime.getTime();
        return between / (1000);
    }
    public static long getBetweenSecond(String startTime,String endTime) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = format.parse(startTime);
        Date endDate = format.parse(endTime);
        long between = endDate.getTime() - startDate.getTime();
        return between / (1000);
    }
}
